import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
import path from "path";
import { createSvgIconsPlugin } from "vite-plugin-svg-icons";
import AutoImport from "unplugin-auto-import/vite";
import Components from "unplugin-vue-components/vite";
import { ElementPlusResolver } from "unplugin-vue-components/resolvers";
import { viteMockServe } from "vite-plugin-mock";
import type { UserConfig, ConfigEnv } from "vite";
import IconsResolver from "unplugin-icons/resolver";
import Icons from "unplugin-icons/vite";
import Inspect from "vite-plugin-inspect";

const pathSrc = path.resolve(__dirname, "src");

// https://vite.dev/config/
export default defineConfig(({ command, mode }: ConfigEnv): UserConfig => {
  console.log("command: ", command);
  console.log("mode: ", mode);
  return {
    plugins: [
      vue({
        script: {
          // 开启defineModel
          defineModel: true,
        },
      }),
      mode === "dev"
        ? viteMockServe({
            // 指定 Mock 配置文件的存放目录
            mockPath: "./mock",
            // 只有dev模式进行拦截
            enable: mode === "dev",
            // 监听 mock 文件变化
            watchFiles: true,
            // 是否开启日志
            logger: true,
          })
        : undefined,
      createSvgIconsPlugin({
        // 指定需要缓存的图标文件夹
        iconDirs: [path.resolve(process.cwd(), "src/assets/svgs")],
        // 指定symbolId格式
        symbolId: "icon-[dir]-[name]",
      }),
      AutoImport({
        resolvers: [
          ElementPlusResolver(),
          IconsResolver({
            prefix: "Icon",
          }),
        ],
      }),
      Components({
        resolvers: [
          IconsResolver({
            enabledCollections: ["ep"],
          }),
          ElementPlusResolver(),
        ],
      }),
      Icons({
        autoInstall: true,
      }),
      Inspect(),
    ],
    css: {
      preprocessorOptions: {
        scss: {
          // 添加默认scss配置文件
          additionalData: `@use "@/styles/main.scss";`,
        },
      },
    },
    resolve: {
      alias: {
        "@": path.resolve(__dirname, "./src"),
      },
    },
  };
});
